#!/usr/bin/perl -w # for syntax highlighting
# CSS-V1

# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
use gri_secmgr qw(get_cookie_value redirect_header);
use gri_user_alert_config;
use gri_macro_handler qw(get_uploaded_file_handle);
use gri_database;
use File::Temp qw(tempfile);
use Digest::SHA1;

sub mm_form_docrepos_replace_document_upload_process {
	my $str="";

	my $link="";
	if(exists($::PARAM{lp})) {
		$link=hex_to_string($::PARAM{lp});
	} 
	# document_sha1 and lname refer to the existing document to replace - 
	# these are necessary as well as the other details to perform the file 'replace'.

	my $max_upload_size=10; # Maximum MB upload size
	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $docrepos=$::GRI_FRONTEND->get_global_directory("docrepos");
	my $DB=new gri_database("$db_dir/gri.db");
	
	my $file_name=$::PARAM{name};
	my $file_description=$::PARAM{description};
	my $file_actual=$::PARAM{actual};
	my $document_sha1=$::PARAM{document_sha1};
	my $fh=get_uploaded_file_handle("actual");
	my ($wfh,$tfile)=tempfile(DIR=>$docrepos, SUFFIX=>".gri");
	my $bytesread; my $buffer;
	my $fsize=0;
	my $ctx = Digest::SHA1->new;
	my $failed=0;
	my $toobig=0;
	while ($bytesread=read($fh,$buffer,1024)) {
	   if(! print $wfh $buffer) {
	   	$failed=1;
		last;
	   }
	   $fsize+=$bytesread;
	   if($fsize>($max_upload_size*1024*1024)) {
	   	$toobig=1;
		last;
	   }
	   $ctx->add($buffer);
	}
	close($fh);
	close($wfh);

	# If the write out failed then we abort before processing
	# it ... deleting the necessary temporary files...
	if($failed) {
		unlink($tfile) if -f $tfile;
		my $serror="Error Uploading Replacement Document";
		my $error="Ran out of disk space whilst uploading document '$file_actual'";
		my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$s.="::document_sha1=$::PARAM{document_sha1}";
		$s.="::lname=$::PARAM{lname}";
		$s.="::lp=$::PARAM{lp}";
		$link.=",previouspage=form_docrepos_replace_document_upload::$s";
		redirect_header(path => $link);
		exit;
	}
	if($toobig) {
		unlink($tfile) if -f $tfile;
		my $serror="Error Uploading Replacement Document";
		my $error="The file '$file_actual' is too large to be uploaded.";
		my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$s.="::document_sha1=$::PARAM{document_sha1}";
		$s.="::lname=$::PARAM{lname}";
		$s.="::lp=$::PARAM{lp}";
		$link.=",previouspage=form_docrepos_replace_document_upload::$s";
		redirect_header(path => $link);
		exit;
	}
	my $digest=$ctx->hexdigest;

	# Get the details of the existing document so it can be added to the same	#
	# sections...
	# Always assume this is a new document - do not look for
	# an existing document.

	my ($a2,$b2)=$DB->get_details(sha1=>$document_sha1,lname=>$::PARAM{lname});
	if($a2!=1) {
		my $serror="Error Uploading Replacement Document";
		my $error=$b2;
		my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$s.="::document_sha1=$::PARAM{document_sha1}";
		$s.="::lname=$::PARAM{lname}";
		$s.="::lp=$::PARAM{lp}";
		$link.=",previouspage=form_docrepos_replace_document_upload::$s";
		redirect_header(path => $link);
		exit;
	}
	my $sec=$b2->{sections};
	my ($a,$b)=$DB->add_new_document(content_sha1=>$digest,sections=>$sec,description=>$file_description, original_name=>$file_actual,docrepos=>$docrepos,temporary_file=>$tfile);
	
	unlink($tfile) if -f $tfile;
	if($a!=1) {
		my $serror="Error Uploading Replacement Document";
		my $error=$lname;
		my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$s.="::document_sha1=$::PARAM{document_sha1}";
		$s.="::lname=$::PARAM{lname}";
		$s.="::lp=$::PARAM{lp}";
		$link.=",previouspage=form_docrepos_replace_document_upload::$s";
		redirect_header(path => $link);
		exit;
	}
	my $new_lname=$b;

	# The replacement document has been added to the repository, so 
	# we've got to move the older document to history. We have to get hold of the
	# original lname first though ...
	($a2,$b2)=$DB->get_olocal_name_via_clocal_name(clocal_name => $::PARAM{lname});
	my $olname;
	if($a2==0) {
		$olname="NONE";
	} else {
		$olname=$b2;
	}

	# Now move the file to history
	print STDERR "move lname=$::PARAM{lname}, sha1=>$::PARAM{document_sha1}, clname=$b, olname=$olname\n";
	($a,$b)=$DB->move_to_history(lname => $::PARAM{lname}, clname => $b, olname => $olname, sha1 => $::PARAM{document_sha1});

	redirect_header(path => $link);
	exit;
}

